package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.GsonUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.CommonPhotoActivity;
import flc.ast.activity.MyAlbumActivity;
import flc.ast.activity.PdfConvertPicActivity;
import flc.ast.activity.PhotoDetailActivity;
import flc.ast.activity.PicConvertPdfActivity;
import flc.ast.adapter.IdPhotoAdapter;
import flc.ast.bean.IdPhotosBean;
import flc.ast.databinding.FragmentToolBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IOUtil;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ToolFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ToolFragment extends BaseNoModelFragment<FragmentToolBinding> {

    private IdPhotoAdapter idPhotoAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_tool;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.llCommonPhoto.setOnClickListener(this);
        mDataBinding.llProfessionalPhoto.setOnClickListener(this);
        mDataBinding.llPdfConvertPic.setOnClickListener(this);
        mDataBinding.llPicConvertPdf.setOnClickListener(this);
        mDataBinding.llPhotoType1.setOnClickListener(this);
        mDataBinding.llPhotoType2.setOnClickListener(this);
        mDataBinding.llPhotoType3.setOnClickListener(this);

        mDataBinding.rvIdPhotoList.setLayoutManager(new LinearLayoutManager(mContext));
        idPhotoAdapter = new IdPhotoAdapter();
        mDataBinding.rvIdPhotoList.setAdapter(idPhotoAdapter);
        idPhotoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llCommonPhoto://常见寸照
                startActivity(CommonPhotoActivity.class);
                break;
            case R.id.llProfessionalPhoto://职业形象照
                MyAlbumActivity.type = 1;
                startActivity(MyAlbumActivity.class);
                break;
            case R.id.llPdfConvertPic://pdf转图片
                getManagePermission();
                break;
            case R.id.llPicConvertPdf://图片转pdf
                getPicturePermission();
                break;
            case R.id.llPhotoType1://寸照
                clearSelection();
                mDataBinding.tvPhotoType1.setTextColor(Color.parseColor("#000000"));
                mDataBinding.ivPhotoType1.setVisibility(View.VISIBLE);
                updateView(0);
                break;
            case R.id.llPhotoType2://考试类
                clearSelection();
                mDataBinding.tvPhotoType2.setTextColor(Color.parseColor("#000000"));
                mDataBinding.ivPhotoType2.setVisibility(View.VISIBLE);
                updateView(1);
                break;
            case R.id.llPhotoType3://证件类
                clearSelection();
                mDataBinding.tvPhotoType3.setTextColor(Color.parseColor("#000000"));
                mDataBinding.ivPhotoType3.setVisibility(View.VISIBLE);
                updateView(2);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        Content.sIdPhotosBean = idPhotoAdapter.getItem(position);
        startActivity(PhotoDetailActivity.class);
    }

    @Override
    protected void initData() {
        updateView(0);
    }

    private void updateView(int position) {
        try {
            InputStream is = mContext.getAssets().open("idPhoto" + position + ".json");
            String json = IOUtil.readStream2Str(is);
            List<IdPhotosBean> idPhotosBeans = GsonUtils.fromJson(json, new TypeToken<List<IdPhotosBean>>() {
            }.getType());
            idPhotoAdapter.setList(idPhotosBeans.subList(0, 5));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void getManagePermission() {
        StkPermissionHelper.reqManageExternalStoragePermission().reqPermissionDesc(getString(R.string.get_manage_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                startActivity(PdfConvertPicActivity.class);
            }
        }).request();
    }

    private void getPicturePermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startActivity(PicConvertPdfActivity.class);
                    }
                }).request();
    }


    private void clearSelection() {
        mDataBinding.tvPhotoType1.setTextColor(Color.parseColor("#757575"));
        mDataBinding.ivPhotoType1.setVisibility(View.GONE);
        mDataBinding.tvPhotoType2.setTextColor(Color.parseColor("#757575"));
        mDataBinding.ivPhotoType2.setVisibility(View.GONE);
        mDataBinding.tvPhotoType3.setTextColor(Color.parseColor("#757575"));
        mDataBinding.ivPhotoType3.setVisibility(View.GONE);
    }

}